package dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import dao.OyxCollectionMessageDao;
import entity.Collection;
import entity.OyxVipLevelShow;
import util.C3P0Util;

public class OyxCollectionMessageDaoImpl implements OyxCollectionMessageDao {

	
	//设计方法用于返回操作数据库的qr对象
		private QueryRunner getQueryRunner(){
			return new QueryRunner(C3P0Util.getDataSource());
		}
	//通过用户id 查询用户的收藏信息
	@Override
	public List getUserCollection(Integer userId,Integer collectionPageNo, Integer collectionPageSize) {
		QueryRunner qr = this.getQueryRunner();
		String sql = "SELECT * FROM `collection` WHERE user_id=?  LIMIT ?,?;";
		List<Collection> userCollectionList = null;
		try {
			userCollectionList = qr.query(sql,new Integer[]{userId,(collectionPageNo-1)*collectionPageSize,collectionPageSize},new BeanListHandler<Collection>(Collection.class));
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return userCollectionList;
	}
	@Override
	public Integer getCollectionRows(Integer userId) {
		QueryRunner qr = this.getQueryRunner();
		//编写sql 语句 
		String sql = "SELECT COUNT(collection_id) FROM `collection` WHERE user_id= ? ;";
		//查询结果  
		Long i = null;
		try {
			i=(Long)qr.query(sql,userId, new ScalarHandler(1));
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return i.intValue();
	}
	
	 //通过用户id及收藏商品编号  删除登录用户对应的收藏商品
	@Override
	public Integer deleteCollection(Integer userId, Integer collection_id) {
		QueryRunner qr = this.getQueryRunner();
		//编写sql 语句 
		String sql = "DELETE FROM `collection` WHERE user_id= ? AND collection_id= ? ;";
		Integer i = 0;
		try {
			i = qr.update(sql,userId,collection_id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	}

}
